/* $Id: imgflip.jquery.js 14 2009-07-09 09:48:56Z geraldb $

Unobtrusive jQuery Resizable Elements

Overview:
	Make resizable elements.

Usage:
	$('selector').resizable();

Parameters:
	target: (element, jQuery) Element to resize, null = selected element
		Default: null
	horizontal: (Bool) Allows horizontal resizing if true
		Default: true
	vertical: (Bool) Allows vertical resizing if true
		Default: true

Authors:
	gburns
*/

(function($) {
$.fn.resizable = function(parameters) {
	var settings = $.extend({
		target: null,
		horizontal: true,
		vertical: true
	}, parameters || {});

	return this.each(function() {
		var mouse_start_x = null;
		var mouse_start_y = null;
		var obj_start_w = null;
		var obj_start_h = null;
		var $this = $(this);
		var $target = settings.target || $this;

		var obj_drag = function(event) {
			if ( settings.horizontal ) {
				$target.css({
					width: obj_start_w + (event.pageX - mouse_start_x)
					});
			}

			if ( settings.vertical ) {
				$target.css({
					height: obj_start_h + (event.pageY - mouse_start_y)
					});
			}
		}

		var return_false = function() { return false; }

		$this
			.mousedown(function(event) {
				event.preventDefault();
				mouse_start_x = event.pageX;
				mouse_start_y = event.pageY;

				obj_start_w = parseInt($target.innerWidth()) - parseInt($target.css('padding-left')) - parseInt($target.css('padding-right'));
				obj_start_h = parseInt($target.innerHeight()) - parseInt($target.css('padding-top')) - parseInt($target.css('padding-bottom'));

				var $body = $(document.body);
				var old_moz_user_select = $body.css('MozUserSelect');
				$body.css({MozUserSelect: 'none'});

				$body
					.mousemove(obj_drag)
					.mouseup(function() {
						$body.css({MozUserSelect: ''});

						$body
							.unbind('mousemove', obj_drag);

						if ($.browser.msie) {
							$body
								.unbind('dragstart', return_false)
								.unbind('selectstart', return_false);
						}
					});

				if ($.browser.msie) {
					$body
						.bind('dragstart', return_false)
						.bind('selectstart', return_false);
				}
			});
	});
}
})(jQuery);